//
//  CCAlertModel.m
//  Activity
//
//  Created by its on 15/6/10.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCAlertModel.h"
#import "CCError.h"
#import <LinqToObjectiveC/NSArray+LinqExtensions.h>

@implementation CCAlertModel

- (RACSignal *) getUnreadCommentFromServer {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [AVStatus getUnreadStatusesCountWithType:@"comment" andCallback:^(NSInteger number, NSError *error) {
      if ( !error ) {
        [subscriber sendNext:@[ERROR(6000),@(number)]];
        [subscriber sendCompleted];
      } else {
        [subscriber sendNext:@[ERROR(6001), @(0)]];
        [subscriber sendCompleted];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}


- (RACSignal *) getUnreadReplyFromServer {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    [AVStatus getUnreadStatusesCountWithType:@"reply" andCallback:^(NSInteger number, NSError *error) {
      if ( !error ) {
        [subscriber sendNext:@[ERROR(6000),@(number)]];
        [subscriber sendCompleted];
      } else {
        [subscriber sendNext:@[ERROR(6002), @(0)]];
        [subscriber sendCompleted];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}

- (NSArray *) getUnreadCommentFromLocal {
  NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *path = [NSString stringWithFormat:@"%@/count.plist", documentsDirectory];
  NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
  if (dic == nil) {
    return @[ERROR(6000),@(0)];
  } else {
    return @[ERROR(6000), dic[@"unreadComment"]];
  }
}

- (NSArray *) getUnreadReplyFromLocal {
  NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *path = [NSString stringWithFormat:@"%@/count.plist", documentsDirectory];
  NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
  if (dic == nil) {
    return @[ERROR(6000),@(0)];
  } else {
    return @[ERROR(6000), dic[@"unreadReply"]];
  }
}

- (RACSignal *) getCommentList:(int) from limit:(int)limit owner:(AVObject *)owner {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVStatusQuery *query = [AVStatus inboxQuery:@"comment"];
    query.owner = owner;
    query.sinceId = from;
    query.maxId = from + limit;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if ( !error ) {
        NSArray *results = [objects linq_select:^id(id item) {
          return [item dictionaryForObject];
        }];
        results = [self objectForResponse:results andKlass:[CCReplyAlert class] error:NULL];
        [subscriber sendNext:@[results, ERROR(6000)]];
        [subscriber sendCompleted];
      } else {
        [subscriber sendNext:@[[NSNull null],ERROR(6002)]];
        [subscriber sendCompleted];
      }

    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}

- (RACSignal *) getReplyList:(int) from limit:(int)limit owner:(AVObject *)owner {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVStatusQuery *query = [AVStatus inboxQuery:@"reply"];
    query.owner = owner;
    query.sinceId = from;
    query.maxId = from + limit;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if ( !error ) {
        NSArray *results = [objects linq_select:^id(id item) {
          return [item dictionaryForObject];
        }];
        results = [self objectForResponse:results andKlass:[CCReplyAlert class] error:NULL];
        [subscriber sendNext:@[results, ERROR(6000)]];
        [subscriber sendCompleted];
      } else {
        [subscriber sendNext:@[[NSNull null],ERROR(6001)]];
        [subscriber sendCompleted];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}

- (id)objectForResponse:(id)response andKlass:(Class)klass error:(NSError **)error {
  id object = [response isKindOfClass:[NSArray class]] ?
  [MTLJSONAdapter modelsOfClass:klass fromJSONArray:response error:error] :
  [MTLJSONAdapter modelOfClass:klass fromJSONDictionary:response error:error];
  return object;
}

@end


@implementation CCCommentAlert

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
  return @{
           @"activityId" : @"activityId",
           @"author" : @"author",
           @"commentId" : @"commentId"
           };
}

@end

@implementation CCReplyAlert

+ (NSDictionary *) JSONKeyPathsByPropertyKey {
  return @{
           @"activityId" : @"activityId",
           @"author" : @"author",
           @"commentId" : @"commentId"
           };
}
@end